package com.tongren.framework.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.tongren.framework.R;
import com.tongren.framework.callback.JsonCallback;
import com.tongren.framework.core.BaseActivity;
import com.tongren.framework.core.CustomApplication;
import com.tongren.framework.model.BaseResult;
import com.tongren.framework.model.BaseUser;
import com.tongren.framework.sample.SampleTabActivity;
import com.tongren.framework.sample.WelcomeActivity;

import java.util.ArrayList;

import static com.tongren.framework.constants.ConstantValues.BASE_URL;

//import butterknife.BindView;

public class LoginActivity extends BaseActivity implements View.OnClickListener{

//    Button btn_register;
    Button btn_login;
    EditText userName;
    EditText password;
    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_login,-1,MODE_NONE);
    }

    @Override
    protected void setUpView() {
//        btn_register = (Button) findViewById(R.id.btn_register);
//        btn_register.setOnClickListener(this);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        userName = (EditText)findViewById(R.id.userName);
        password = (EditText)findViewById(R.id.password);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
//            case R.id.btn_register:
//                Intent intnet = new Intent(this,RegisterActivity.class);
//                startActivity(intnet);
//                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(userName.getText())){
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
                    break;

                }
                if (TextUtils.isEmpty(password.getText())){
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
                    break;
                }

                OkGo.<BaseResult<BaseUser>>post(BASE_URL+"loginMobile.ajax")//请求方式和请求url
                .tag(this)//请求的 tag, 主要用于取消对应的请求
//                .headers("header1", "headerValue1")//

                .params("yhbm", userName.getText().toString())//
                .params("yhmm", password.getText().toString())//
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)  //缓存模式先使用缓存,然后使用网络数据
                .execute(new JsonCallback<BaseResult<BaseUser>>() {
                    @Override
                    public void onSuccess(Response<BaseResult<BaseUser>> response) {
    //                        handleResponse(response);
                        String str = response.toString();
                        String code = response.body().code;
                        String massege = response.body().massege;
                        BaseUser user = response.body().result;
                        CustomApplication.getInstance().setUserModel(user);// 存储对象
                        startActivity(new Intent(LoginActivity.this, SampleTabActivity.class));
                        finish();
                    }

                    @Override
                    public void onCacheSuccess(Response<BaseResult<BaseUser>> response) {
                        //一般来说,只需呀第一次初始化界面的时候需要使用缓存刷新界面,以后不需要,所以用一个变量标识
//                        if (!isInitCache) {
//                            //一般来说,缓存回调成功和网络回调成功做的事情是一样的,所以这里直接回调onSuccess
                            onSuccess(response);
//                            isInitCache = true;
//                        }
                    }

                    @Override
                    public void onError(Response<BaseResult<BaseUser>> response) {
//                            handleError(response);
//                        recycler.onRefreshCompleted();
                    }
                });

        }
    }
}
